package com.qdxwx.data.service.manager;

import com.qdxwx.common.exception.BusinessException;
import com.qdxwx.common.page.PageRequest;
import com.qdxwx.data.mapper.manager.ManagerPostMapper;
import com.qdxwx.data.mapper.manager.PostMapper;
import com.qdxwx.models.manager.Post;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


/**
 * 岗位信息 服务层处理
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Service
public class PostServiceImpl implements PostService {
    @Autowired
    private PostMapper postMapper;
    @Autowired
    private ManagerPostMapper userPostMapper;

    @Override
    public List<Post> getList(Post post, PageRequest page) {
        return postMapper.selectList(post, page);
    }

    @Override
    public int count(Post post) {
        return postMapper.selectCount(post);
    }

    @Override
    public Post get(Integer postId) {
        return postMapper.selectPostById(postId);
    }

    @Override
    public int deleteByIds(Integer[] postIds) throws BusinessException {
        if (postIds.length < 1) return 0;
        for (Integer postId : postIds) {
            Post post = get(postId);
            if (countUserPostById(postId) > 0) {
                throw new BusinessException(String.format("%1$s已分配,不能删除", post.getPostName()));
            }
        }
        return postMapper.deletePostByIds(postIds);
    }

    @Override
    public int insert(Post post) {
        post.setUpdateTime(LocalDateTime.now());
        post.setCreateTime(LocalDateTime.now());
        return postMapper.insertPost(post);
    }

    @Override
    public int update(Post post) {
        post.setUpdateTime(LocalDateTime.now());
        return postMapper.updatePost(post);
    }

    @Override
    public int updateDisabled(Post post) {
        if(post.getPostId() < 1) return 0;
        if(post.getIsDisabled() == null) return 0;
        return postMapper.updateDisabled(post);
    }

    @Override
    public int countUserPostById(Integer postId) {
        return userPostMapper.countManagerPostById(postId);
    }

    @Override
    public boolean checkPostName(String postName) {
        Post info = new Post();
        info.setPostName(postName);
        return postMapper.selectCount(info) > 0;
    }

    @Override
    public boolean checkPostCode(String postCode) {
        Post info = new Post();
        info.setPostCode(postCode);
        return postMapper.selectCount(info) > 0;
    }
}
